package gbench.appdemo.crawler;
import org.junit.jupiter.api.Test;

import static gbench.common.matlib.MatlibCanvas.println;
import static gbench.common.tree.LittleTree.MFT;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.stream.*;

public class JunitDownload {
    
    /**
     * 下载图片
     * @author gbench
     *
     */
     public static class Downloader {

         /**
          * 下载文件
          * @param destUrl
          */
         public void saveToFile(String destUrl,String path) {
             FileOutputStream fos = null;
             BufferedInputStream bis = null;
             HttpURLConnection httpUrl = null;
             URL url = null;
             int BUFFER_SIZE = 1024;
             byte[] buf = new byte[BUFFER_SIZE];
             int size = 0;
             
             final var f = new File(path);
             if(!f.getParentFile().exists()) {
                 f.getParentFile().mkdirs();
             } // checke the directory existence status
             
             try {
                 url = new URL(destUrl);
                 httpUrl = (HttpURLConnection) url.openConnection();
                 httpUrl.connect();
                 bis = new BufferedInputStream(httpUrl.getInputStream());
                 fos = new FileOutputStream(path);
                 while ((size = bis.read(buf)) != -1) {
                     fos.write(buf, 0, size);
                 }
                 fos.flush();
             } catch (IOException e) {
                 e.printStackTrace();
             } catch (ClassCastException e) {
             } finally {
                 try {
                     fos.close();
                     bis.close();
                     httpUrl.disconnect();
                 } catch (IOException e) {
                     e.printStackTrace();
                 } catch (NullPointerException e) {
                 }
             }
         }
     }
    
    
    @Test
    public void foo() {
       final var downloader =  new Downloader();
       for(int no =0;no<37;no++) {// the file group codes
           final var filegroup = no;
           Stream.iterate(1, i->i<30,i->i+1).forEach(i->{
               final var url = MFT("{0}/{1}/{2}.pdf", url_prefix,filegroup,i);
               final var path = MFT("{0}/{1}/{2}.pdf", path_prefix,filegroup,i);
               println(url,path);
               downloader.saveToFile(url, path);
           });
       }
    }
    
    String url_prefix = "http://www.shfe.com.cn/jrysp";
    String path_prefix = "D:/sliced/desktop/ebook/金融衍生品";

}
